﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Genesis
{
    public partial class frmdirectory : Form
    {
        int FileCount = 0;
        string[] Files;
        public frmdirectory()
        {
            InitializeComponent();
        }

        private void frmdirectory_Load(object sender, EventArgs e)
        {
            FolderDiag.ShowDialog();
            if (Directory.Exists(FolderDiag.SelectedPath))
            {
                string[] mp3Files = Directory.GetFiles(FolderDiag.SelectedPath, "*.mp3", SearchOption.AllDirectories);
                string[] wavFiles = Directory.GetFiles(FolderDiag.SelectedPath, "*.wav", SearchOption.AllDirectories);
                string[] oggFiles = Directory.GetFiles(FolderDiag.SelectedPath, "*.ogg", SearchOption.AllDirectories);

                FileCount = mp3Files.Length + wavFiles.Length + oggFiles.Length;

                if (FileCount > 0)
                {
                    pBar.Maximum = FileCount;
                    Files = new string[FileCount];

                    for (int i = 0; i < mp3Files.Length; i++)
                        Files[i] = mp3Files[i];
                    for (int i = mp3Files.Length; i < wavFiles.Length + mp3Files.Length; i++)
                        Files[i] = wavFiles[i - mp3Files.Length];
                    for (int i = mp3Files.Length + wavFiles.Length; i < oggFiles.Length + wavFiles.Length + mp3Files.Length; i++)
                        Files[i] = oggFiles[i - wavFiles.Length - mp3Files.Length];
                    FileWorker.RunWorkerAsync();
                }
                else
                {
                    MessageBox.Show("No Media Files Were Found");
                    this.DialogResult = DialogResult.Abort;
                }
            }
            else
                this.DialogResult = DialogResult.Abort;
        }

        public delegate void UpdateTextCallback(string text);
        private void UpdateText(string text)
        {
            lblfile.Text = text;
        }

        private void FileWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < Files.Length; i++)
            {
                Library.AddSong(Uri.UnescapeDataString(Files[i]));
                FileWorker.ReportProgress(i);
            }
        }
        private void FileWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            pBar.Value = e.ProgressPercentage;
            lblfile.Text = Files[pBar.Value];
        }
        private void FileWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}
